package com.skycong.hello77.tabfragment.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.skycong.hello77.tabfragment.activity.TabFragmentActivity;

import java.util.List;

/**
 * Created by Kiki on 2018/6/21.
 */

public class TabFragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> list;
    private TabFragmentActivity activity;

    public TabFragmentAdapter(FragmentManager fm,List<Fragment> list,TabFragmentActivity activity) {
        super(fm);
        this.list = list;
        this.activity = activity;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public Fragment instantiateItem(ViewGroup container, int position) {
       Fragment fragment = (Fragment) super.instantiateItem(container,position);
        activity.getSupportFragmentManager().beginTransaction().show(fragment).commitAllowingStateLoss();
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Fragment fragment = list.get(position);
        activity.getSupportFragmentManager().beginTransaction().hide(fragment).commitAllowingStateLoss();
    }
}
